package com.ruoyi.reunion.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.reunion.domain.ActivityComment;
import com.ruoyi.reunion.domain.ActivityParticipant;
import com.ruoyi.reunion.domain.Notification;
import com.ruoyi.reunion.domain.Vo.ActivityParticipantUserInfo;
import com.ruoyi.reunion.mapper.ActivityCommentMapper;
import com.ruoyi.reunion.service.IActivityCommentService;
import com.ruoyi.reunion.service.IActivityParticipantService;
import com.ruoyi.reunion.service.INotificationService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.reunion.domain.Activity;
import com.ruoyi.reunion.service.IActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动Controller
 * 
 * @author ccccc
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/reunion/activity")
public class ActivityController extends BaseController
{
    @Autowired
    private IActivityService activityService;

    @Autowired
    private IActivityParticipantService activityParticipantService;


    @Autowired
    private ISysUserService sysUserService;


    @Autowired
    private IActivityCommentService activityCommentService;



    @Autowired
    private INotificationService notificationService;

    //查询所有评论
    @GetMapping("/getAllActivityComment/{aid}")
    public AjaxResult getAllActivityComment(@PathVariable Long aid) {
        ActivityComment activityComment = new ActivityComment();
        activityComment.setActivityId(aid);
        List<ActivityComment> activityComments = activityCommentService.selectActivityCommentList(activityComment);
        return success(activityComments);
    }



    //添加活动评论
    @PostMapping("/addComment")
    private AjaxResult addActivityComment(@RequestBody ActivityComment activityComment) {
        ActivityComment activityComment1 = new ActivityComment();
        activityComment1.setId(1L);
        activityComment1.setActivityId(activityComment.getActivityId());
        activityComment1.setUserId(getUserId());
        activityComment1.setContent(activityComment.getContent());
//        activityComment1.setCreateTime(new Date());
        activityComment1.setUsername(getUsername());
        activityComment1.setAvatar(getLoginUser().getUser().getAvatar());
        activityComment1.setStatus(1L);



        System.out.println(activityComment1);
        activityCommentService.insertActivityComment(activityComment1);

        return AjaxResult.success();
    }






    //查询活动id对应预约人信息
    @GetMapping("/getActivityParticipantUserInfo/{aid}")
    public AjaxResult getActivityParticipantUserInfo(@PathVariable Long aid) {
        ActivityParticipant activityParticipant = new ActivityParticipant();
        activityParticipant.setActivityId(aid);
        activityParticipant.setStatus(1L);
        List<ActivityParticipant> activityParticipants = activityParticipantService.selectActivityParticipantList(activityParticipant);

        ArrayList<ActivityParticipantUserInfo> activityParticipantUserInfos = new ArrayList<>();
        for (ActivityParticipant activityParticipant1 : activityParticipants) {
            ActivityParticipantUserInfo activityParticipantUserInfo = new ActivityParticipantUserInfo();
            activityParticipantUserInfo.setUserId(activityParticipant1.getUserId());
            SysUser sysUser = sysUserService.selectUserById(activityParticipant1.getUserId());
            activityParticipantUserInfo.setUsername(sysUser.getUserName());
            activityParticipantUserInfo.setAvatar(sysUser.getAvatar());
            activityParticipantUserInfo.setParticipantRole(activityParticipant1.getRole());
            activityParticipantUserInfos.add(activityParticipantUserInfo);
        }
        return AjaxResult.success(activityParticipantUserInfos);
    }

    //根据 活动id 参加活动
    @GetMapping("/addActivityParticipant/{aid}")
    public AjaxResult addActivityParticipant(@PathVariable Long aid) {
        //
        ActivityParticipant activityParticipant = new ActivityParticipant();
        activityParticipant.setActivityId(aid);
        activityParticipant.setUserId(getUserId());

        //先查询是否已经预约
        if (!activityParticipantService.selectActivityParticipantList(activityParticipant).isEmpty()) {
            return AjaxResult.warn("您已经预约过该活动");
        }

        Notification notification = new Notification();
        notification.setType(2L);
        notification.setContent("您已成功报名活动，请等待审核");
        notification.setTargetId(getUserId());
        notification.setTitle("活动报名通知");
        notification.setCreateTime(new Date());
        notificationService.insertNotification(notification);

        activityParticipant.setCreateTime(new Date());
        activityParticipant.setStatus(0L);
        return toAjax(activityParticipantService.insertActivityParticipant(activityParticipant));
    }





    //查询活动id对应预约人数
    @GetMapping("/getActivityParticipantCount/{id}")
    public AjaxResult getActivityParticipantCount(@PathVariable Long id) {

        ActivityParticipant activityParticipant = new ActivityParticipant();
        activityParticipant.setActivityId(id);
        activityParticipant.setStatus(1L);

        return success(activityParticipantService.selectActivityParticipantList(activityParticipant).size());
    }
    //添加活动信息
    @PostMapping("/addUActivity")
    public AjaxResult addUActivity(@RequestBody Activity activity) {
        //

        activity.setOrganizer(getUsername());
        activity.setCreateTime(new Date());
        activity.setStatus(0L);



        return toAjax(activityService.insertActivity(activity));
    }


    //查询所有活动
    @GetMapping("/getAllActivity")
    public AjaxResult getAllActivity(){
        List<Activity> activityList = activityService.selectActivityList(new Activity());

        return success(activityList);
    }

    /**
     * 查询活动列表
     */
    @PreAuthorize("@ss.hasPermi('reunion:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(Activity activity)
    {
        startPage();
        List<Activity> list = activityService.selectActivityList(activity);
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @PreAuthorize("@ss.hasPermi('reunion:activity:export')")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Activity activity)
    {
        List<Activity> list = activityService.selectActivityList(activity);
        ExcelUtil<Activity> util = new ExcelUtil<Activity>(Activity.class);
        util.exportExcel(response, list, "活动数据");
    }

    /**
     * 获取活动详细信息
     */
//    @PreAuthorize("@ss.hasPermi('reunion:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(activityService.selectActivityById(id));
    }

    /**
     * 新增活动
     */
    @PreAuthorize("@ss.hasPermi('reunion:activity:add')")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Activity activity)
    {
        return toAjax(activityService.insertActivity(activity));
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('reunion:activity:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Activity activity)
    {
        return toAjax(activityService.updateActivity(activity));
    }

    /**
     * 删除活动
     */
    @PreAuthorize("@ss.hasPermi('reunion:activity:remove')")
    @Log(title = "活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(activityService.deleteActivityByIds(ids));
    }
}
